package com.xuexiang.xui_lib.component.progress.ratingbar;

import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.components.AttrSet;
import ohos.app.Context;

/**
 * RotationRatingBar
 *
 * @author xuexiang
 * @since 2019-06-06
 */
public class RotationRatingBar extends AnimationRatingBar {
    // Control animation speed
    private static final long ANIMATION_DELAY = 15;

    /**
     * RotationRatingBar
     *
     * @param context
     */
    public RotationRatingBar(Context context) {
        super(context);
        initData();
    }

    /**
     * RotationRatingBar
     *
     * @param context
     * @param attrs
     */
    public RotationRatingBar(Context context, AttrSet attrs) {
        super(context, attrs);
        initData();
    }

    /**
     * RotationRatingBar
     *
     * @param context
     * @param attrs
     * @param defStyleAttr
     */
    public RotationRatingBar(Context context, AttrSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void initData() {
        setScaleOk();
    }

    @Override
    protected void emptyRatingBar() {
        // Need to remove all previous runnable to prevent emptyRatingBar and fillRatingBar out of sync
        if (mRunnable != null) {
            handler.removeTask(mRunnable, mRunnableToken);
        }

        long delay = 0;
        for (final PartialView partialView : mPartialViews) {
            handler.postTask(() -> partialView.setEmpty(), delay += 5);
        }
    }

    @Override
    protected void fillRatingBar(final float rating) {
        // Need to remove all previous runnable to prevent emptyRatingBar and fillRatingBar out of sync
        if (mRunnable != null) {
            handler.removeTask(mRunnable, mRunnableToken);
        }

        for (final PartialView partialView : mPartialViews) {
            final int ratingViewId = (int) partialView.getTag();
            final double maxIntOfRating = Math.ceil(rating);

            if (ratingViewId > maxIntOfRating) {
                partialView.setEmpty();
                continue;
            }

            mRunnable = getAnimationRunnable(rating, partialView, ratingViewId, maxIntOfRating);
            postRunnable(mRunnable, ANIMATION_DELAY);
        }
    }

    private Runnable getAnimationRunnable(final float rating, final PartialView partialView, final int ratingViewId,
                                          final double maxIntOfRating) {
        return new Runnable() {
            @Override
            public void run() {
                if (ratingViewId == maxIntOfRating) {
                    partialView.setPartialFilled(rating);
                } else {
                    partialView.setFilled();
                }

                if (ratingViewId == rating) {
                    AnimatorProperty animator = partialView.createAnimatorProperty();
                    animator.setDuration(750);
                    animator.rotate(360.0f);
                    animator.setCurveType(Animator.CurveType.ACCELERATE_DECELERATE);

                    animator.start();
                }
            }
        };
    }
}